#include <rtthread.h>
#include "webclient.h"
#include "gbk2utf8.h"

#define DBG_TAG   "api"
#define DBG_LVL   DBG_LOG
#include <rtdbg.h>

#define GET_HEADER_BUFSZ               512
#define GET_RESP_BUFSZ                 1024

#define API_FIRST_SHOWAPI //定义优先使用万维易源，否则默认使用聚合数据

#define SHOWAPI_HTTP_URL        "http://route.showapi.com/" //万维易源
#define JUHE_HTTP_URL           "http://apis.juhe.cn/"      //聚合数据

//天气API接口
#define JUHE_WEATHRE_KEY "4c7700c86dfd38d4d67c18650ee56026"

//static unsigned char resp_buffer[GET_RESP_BUFSZ] = {0};

static unsigned char *user_http_get(const char *uri)
{
    struct webclient_session* session = RT_NULL;
    unsigned char *resp_buffer = RT_NULL;
    int bytes_read, resp_status;
    int content_length = -1;
    uint32_t recv_len = 0, resp_buff_size = GET_RESP_BUFSZ;

    resp_buffer = (unsigned char *) web_malloc(GET_RESP_BUFSZ);
    if (resp_buffer == RT_NULL)
    {
        LOG_E("no memory for receive buffer.\n");
        goto __exit;
    }

    /* create webclient session and set header response size */
    session = webclient_session_create(GET_HEADER_BUFSZ);
    if (session == RT_NULL)
    {
        LOG_E("no memory for header buffer.\n");
        goto __exit;
    }

    /* send GET request by default header */
    if ((resp_status = webclient_get(session, uri)) != 200)
    {
        LOG_E("webclient GET request failed, response(%d) error.\n", resp_status);
        goto __exit;
    }

    content_length = webclient_content_length_get(session);

    do
    {
        if(resp_buff_size < (recv_len + (unsigned int)content_length > GET_RESP_BUFSZ ? GET_RESP_BUFSZ : content_length))    //数组不够
        {
            resp_buff_size += GET_RESP_BUFSZ;
            unsigned char *temp =  web_realloc(resp_buffer,resp_buff_size);
            if(temp == RT_NULL)
            {
                LOG_E("no memory for receive buffer.\n");
                goto __exit;
            }
            else
                resp_buffer = temp;
        }
        bytes_read = webclient_read(session, resp_buffer, (unsigned int)content_length > GET_RESP_BUFSZ ? GET_RESP_BUFSZ : content_length);
        if (bytes_read <= 0)
            break;
        recv_len += bytes_read;
        content_length -= bytes_read;
    } while (content_length >= 0);

    resp_buffer[recv_len] = 0;
#if (DBG_LVL >= DBG_LOG)
    rt_kprintf("resp data:\n%s\n", resp_buffer);
#endif
    if (session != RT_NULL)
        webclient_close(session);
    return resp_buffer;

__exit:
    if (session != RT_NULL)
        webclient_close(session);
    if (resp_buffer != RT_NULL)
        web_free(resp_buffer);

    return RT_NULL;
}

char * http_weather_get(char *city)
{
    const char default_city[10] = {"杭州"};
    char *uri = RT_NULL;
    char get[150] = JUHE_HTTP_URL"simpleWeather/query?city=";
    uint16_t len = rt_strlen(get);
    unsigned char *dat = RT_NULL;

    if (city != RT_NULL)
    {
        if(gbk2utf8(&uri, city, rt_strlen((const char *)city)) == 0)
        {
            LOG_E("convert gbk to utf8 failed.\n");
            if (uri != RT_NULL)
                rt_free(uri);
            return RT_NULL;
        }
    }
    else
        uri = (char *)default_city;
    for(uint8_t i = 0;i < rt_strlen(uri);i ++)
        rt_snprintf(&get[len + 3 * i], sizeof(get) - len - 3 * i - 1,"%%%02X", uri[i]);
    if (city != RT_NULL && uri != RT_NULL)
        rt_free(uri);
    uri = RT_NULL;
    rt_snprintf(&get[rt_strlen(get)], sizeof(get) - rt_strlen(get) - 1, "%s", "&key="JUHE_WEATHRE_KEY);
    uri = web_strdup(get);
    if(uri == RT_NULL)
    {
        LOG_E("no memory for create get request uri buffer.\n");
        return RT_NULL;;
    }

    dat = user_http_get(uri);
    web_free(uri);
    return (char *)dat;
    if(dat != RT_NULL)
        web_free(dat);

    web_free(uri);
}

#ifdef FINSH_USING_MSH
void weather_get(int argc, char **argv)
{
    char *dat = RT_NULL, *city = RT_NULL;;

    if (argc == 1 || argc == 2)
    {
        if (argc == 2)
            city = argv[1];

        dat = http_weather_get(city);

        if(dat != RT_NULL)
            web_free(dat);
    }
    else
        rt_kprintf("weather_get [city]     - get weathre of the city.\n");
}
MSH_CMD_EXPORT(weather_get, get the weather of city);
#endif
